﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;
using InformationSystem.BLL.Core.Model;
using System.Web.Configuration;
using System.Timers;
using InformationSystem.WebSite.Utility;
using InformationSystem.BLL.Core.Service;
using System.Text;


namespace InformationSystem.WebSite
{
    public class Global : System.Web.HttpApplication
    {
        private static Timer timer;
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            Application.Add(Common.StartTime, DateTime.Now);
            Application.Add(Common.VisitedCount, 0);
            Visited today = new Visited();
            today.Today = DateTime.Now.Date;
            today.Count = 0;
            Application.Add(Common.VisitedToday, today);
            Application.Add(Common.OnLine, new List<LoginUser>());
            Application.Add(Common.TimeOut, 120);
            Application.Add(Common.ForumPageRecordCount, 20);
            Application.Add(Common.ForumReplyCount, 20);
            //Default
            double threadRefreshTime = 600 * 1000;
            try
            {
                threadRefreshTime = double.Parse(WebConfigurationManager.AppSettings["TreadRefreshTime"]);
                threadRefreshTime *= 1000;
            }
            catch (Exception)
            { }

            try
            {
                Application[Common.ForumPageRecordCount] = int.Parse(WebConfigurationManager.AppSettings["ForumPageRecordCount"]);
            }
            catch (Exception)
            { }

            try
            {
                Application[Common.ForumReplyCount] = int.Parse(WebConfigurationManager.AppSettings["ForumReplyCount"]);
            }
            catch (Exception)
            { }

            //Default
            Application.Add(Common.TimeOut, 70);
            try
            {
                Application[Common.TimeOut] = int.Parse(WebConfigurationManager.AppSettings["TimeOut"]);
            }
            catch (Exception)
            { }

            //Default
            Application[Common.ClientRefreshSpan] = 30 * 1000;
            try
            {
                Application[Common.ClientRefreshSpan] = int.Parse(WebConfigurationManager.AppSettings["RequestSpan"]) * 1000;
            }
            catch (Exception)
            { }

            if (timer == null)
            {
                timer = new Timer(threadRefreshTime);
                timer.AutoReset = true;
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                timer.Start();
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Application.Lock();
            List<LoginUser> useronlinelist = (List<LoginUser>)Application[Common.OnLine];
            OnLineHandler.AutoRemoveTimeOutUser(useronlinelist, (int)Application[Common.TimeOut]);
            Application.UnLock();
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            Application.Clear();
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            
            // Code that runs when a new session is started

        }
        void Session_End(object sender, EventArgs e)
        {

            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }



    }
}
